import dayjs from "dayjs";
import {
  ExamplegetPage,
  Examplesave,
  ExampleDelete
} from "@/api/teacher/example";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";

export function useExample() {
  const form = reactive({
    Long: "",
    Str1: "",
    Money: "",
    RoleType: ""
  });

  const ExampleDto = reactive({
    Id: "",
    Long: "",
    Str1: "",
    Money: "",
    RoleType: "",
    DateTime: "",
    Bool: ""
  });

  const roleTypes = ref([]);
  const dataList = ref([]);
  const loading = ref(true);
  const PanelVisibleisshow = ref(false);
  const title = ref("");
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "Id",
      prop: "Id",
      minWidth: 100
    },
    {
      label: "交易所",
      prop: "Long",
      minWidth: 120
    },
    {
      label: "股票名称",
      prop: "Str1",
      minWidth: 150
    },
    {
      label: "股票代码",
      prop: "Long",
      minWidth: 120
    },
    {
      label: "行情时间",
      minWidth: 180,
      prop: "CDate",
      formatter: ({ CDate }) => dayjs(CDate).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "更新时间",
      minWidth: 180,
      prop: "CDate",
      formatter: ({ CDate }) => dayjs(CDate).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "EMA12",
      prop: "Long",
      minWidth: 120
    },
    {
      label: "EMA26",
      prop: "Long",
      minWidth: 120
    },
    {
      label: "DIF",
      prop: "Long",
      minWidth: 120
    },
    {
      label: "DEA",
      prop: "Long",
      minWidth: 120
    }
  ];

  function PanelVisible(row) {
    row === 0 ? (title.value = "新增") : (title.value = "修改");
    if (row) {
      ExampleDto.Bool = row.Bool;
      ExampleDto.DateTime = row.DateTime;
      ExampleDto.Id = row.Id;
      ExampleDto.Long = row.Long;
      ExampleDto.Money = row.Money;
      ExampleDto.RoleType = row.RoleType;
      ExampleDto.Str1 = row.Str1;
    } else {
      ExampleDto.Bool = "";
      ExampleDto.DateTime = "";
      ExampleDto.Id = "";
      ExampleDto.Long = "";
      ExampleDto.Money = "";
      ExampleDto.RoleType = "";
      ExampleDto.Str1 = "";
    }
    PanelVisibleisshow.value = true;
  }

  async function handleDelete(row) {
    await ExampleDelete(row);
    onSearch();
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    onSearch();
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    const { data } = await ExamplegetPage({
      pageindex: pagination.currentPage,
      pagesize: pagination.pageSize,
      ...form
    });

    dataList.value = data.list;
    pagination.total = data.totalCount;
    loading.value = false;
  }

  async function addExample() {
    const respone = await Examplesave(ExampleDto);
    if (respone.success) {
      onSearch();
      PanelVisibleisshow.value = false;
      ElMessage({
        message: "新增成功",
        type: "success"
      });
    }
  }

  async function updateExample() {
    if (title.value == "修改") {
      const respone = await Examplesave(ExampleDto);
      console.log(ExampleDto);

      if (respone.success) {
        onSearch();
        PanelVisibleisshow.value = false;
        ElMessage({
          message: `${title.value}成功`,
          type: "success"
        });
      }
    } else {
      addExample();
      console.log(ExampleDto);
    }
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    ExampleDto,
    roleTypes,
    PanelVisibleisshow,
    title,
    onSearch,
    resetForm,
    PanelVisible,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    addExample,
    updateExample
  };
}
